#include<iostream>
#include<cstring>
#include<string>
#include<vector>
#include<queue>

using std::cin;
using std::cout;
using std::vector;
using std::endl;
using std::queue;

template<typename T2>
void Input(vector<vector<T2>> &g, T2 &start, T2 &fin) {
    int n, m;
    cin >> n;
    cin >> m;
    vector<vector<T2>> table;
    table.resize(n);
    T2 x, y;
    cin >> x >> y;
    --x, --y;
    start = (x * m) + y;
    cin >> x >> y;
    --x, --y;
    fin = (x * m) + y;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            T2 next;
            cin >> next;
            table[i].push_back(next);
        }
    }
    g.resize(n * m);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if (!table[i][j]) {
                if (i && !table[i - 1][j]) {
                    g[m * i + j].push_back(m * (i - 1) + j);
                }
                if (i != (n - 1) && !table[i + 1][j]) {
                    g[m * i + j].push_back(m * (i + 1) + j);
                }
                if (j && !table[i][j - 1]) {
                    g[m * i + j].push_back(m * (i)+j - 1);
                }
                if (j != (m - 1) && !table[i][j + 1]) {
                    g[m * i + j].push_back(m * i + j + 1);
                }
            }
        }
    }
}

template<typename T1>
void Bfs(const vector<vector<T1>> &g, T1 start, vector<T1> &length) {
    queue<T1> q;
    q.push(start);

    while (!q.empty()) {
        T1 v = q.front();
        q.pop();
        for (size_t i = 0; i < g[v].size(); ++i) {
            T1 to = g[v][i];
            if (to != start && (length[to] == 0 ||
                length[to] > length[v] + 1)) {
                q.push(to);
                length[to] = length[v] + 1;
            }
        }
    }
}

int main() {
    vector<vector<int>> g;
    int start, fin;
    Input(g, start, fin);
    vector<int> length;
    length.resize(g.size());

    Bfs(g, start, length);
    if (start == fin) {
        cout << "0";
    }
    else {
        if (!length[fin]) {
            cout << "NO";
        }
        else
        {
            cout << length[fin];
        }
    }
}